package demo.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import demo.dao.UserDao;
import demo.data.UserDO;
import demo.data.UserDTO;
import demo.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private UserDao userDao;
	
	
	@RequestMapping("/echo")
	public UserDO echo(@RequestBody UserDO user) {	
		return user;
	}
	
	
	
	@RequestMapping("/getById/{id}")
	public UserDO getById(@PathVariable Long id) {
		return userService.getById(id);
	}
	
	
	@RequestMapping("/insert")
	public UserDO insert(UserDO user) {
		userService.insert(user);
		return user;
	}
	
	
	@RequestMapping("/update")
	public UserDO update(UserDO user) {
		userService.update(user);
		return user;
	}
	
	@RequestMapping("/update2")
	public UserDO update2(@RequestBody UserDO user) {
		userService.update(user);
		return user;
	}
	
	@RequestMapping("/update3")
	public UserDO update3(UserDO user) {
		// 直接调用DAO ,  使用set标签 动态更新 自动去掉多余的 逗号 
		userDao.update2(user);
		return user;
	}
	
	@RequestMapping("/delete")
	public String delete(Long id){
		userService.delete(id);
		return "delete done,id="+id;
	}
	

	@RequestMapping("/getAllUsers")
	public List<UserDO> getAllUsers() {
		return userService.getAllUsers();
	}
	
	@RequestMapping("/getAllUserDTOs")
	public List<UserDTO> getAllUserDTOs() {
		return userService.getAllUserDTOs();
	}
	
	
	@RequestMapping("/addUsersTrans")
	public String addUsersTrans() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		userService.addUsersTrans(map);
		return "addUsersTrans done,"+new Date();
	}
	
	@RequestMapping("/addUsersNoTrans")
	public String addUsersNoTrans() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		userService.addUsersNoTrans(map);
		return "addUsersNoTrans done,"+new Date();
	}
	
	@RequestMapping("/getUserServiceInfo")
	public String getUserServiceInfo() throws Exception{
		
		return userService.getClass().toString();
	}

}